var util = require('../../utils/util.js');
var app = getApp()
Page({
  data: {
    othertop1: '我们正在整合升级供应链来满足您更多的需求',//其他区域
    othertop2: '如果您的需求我们可以给与搭配或找到合适的供应链',//其他区域
    othertop3:'我们则会在第一时间与您沟通进行服务',//其他区域
    otherInput1: '我想要的品类是什么',
    otherInput11: 0,
    otherInput2: '我的预算是多少',
    otherInput22: 0,
    otherInput3: '我想要穿着使用的场合是什么',
    otherInput33: 0,
    otherInput4: '备注（例：参考品牌***）',
    otherInput44:0,
    // 导航按钮
    currentTab: 0,
    sortValue: 0, // 选中的tab
    selectedCateList: [],
    priceSlected:'',
    numberSlected:'',
    tagList: [],
    selected: -1,
    selectedImg: false,
    successSubmit: 1,
    requirementId:'',//获取需求id
    mohuanUser:'',
    disabled:true,
    btnTxt:'提交需求进入在线交流'
  },
  onLoad: function(options) {    
    // 页面初始化 options为页面跳转所带来的参数   
  },
  onShow:function(){
    var that = this 
      
    var IsClickContactOther = wx.getStorageSync("IsClickContactOther");
    console.log("== IsClickContactOther==" + IsClickContactOther)
    if (IsClickContactOther == "1") {
      console.log("Huqudaocontact")
      wx.removeStorage({
        key: 'IsClickContactOther',
        success: function (res) { },
      })
      that.setData({
        btnTxt: '需求已提交请等待回复',
        blackText: false,
      })

    } else {
      console.log('weihuoqudaocontact')
      that.setData({
        otherInput1: '我想要的品类是什么',
        otherInput11: 0,
        otherInput2: '我的预算是多少',
        otherInput22: 0,
        otherInput3: '我想要穿着使用的场合是什么',
        otherInput33: 0,
        otherInput4: '备注（例：参考品牌***）',
        otherInput44: 0,        
        btnTxt: '提交需求进入在线交流'
      })
    }


    // 其他区表单  
    // 获取需求id
    //  获取需求id 其他区提交需求时调用 
    util.getrequirementId(`/c2mwebservice/api/v2/public/customization/getDemandId`, function (res) {
      // console.log(res)
      if (res.returnCode == '200') {
        that.data.requirementId = res.returnData.demandId
        that.setData({
          requirementId: res.returnData.demandId
        })
      }
      console.log(that.data.requirementId)
    }, function (error) {
      console.log(error)
      wx.hideToast()
    })     
    // 获取品类接口
    util.getcateList(`/c2mwebservice/api/v2/public/customization/categoryBudget`, function (res) {
      console.log(res)
      if (res.returnCode == '200') {
        that.setData({
          tagList: res.returnData
        })
      }
    }, function (error) {
      console.log(error)
      wx.hideToast()
    })
 
  },
  
  // 点击标签更改样式
  selectMoney: function(e) {    
    var that = this;
    var cateindex = e.currentTarget.dataset.cateindex;
    this.data.tagList[this.data.currentTab].list[cateindex].ischeck = true
    that.setData({
      tagList :  this.data.tagList
    })
    console.log(that.data.tagList)
  },
  // 底部滑动选择器更改列值事件
  // bindcolumnchange:function(e){
  //   console.log(e)
  //   console.log('********修改的列为', e.detail.column);
  // },
  // 底部滑动选择器确定出发事件
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value) 
    var sortid = e.currentTarget.dataset.sortid;//获取父分类id
    var sortname = e.currentTarget.dataset.sortname;//获取父分类名称

     var cateid = e.currentTarget.dataset.cateid;//获取品类id
    var catename = e.currentTarget.dataset.catename;//获取品类名称
    var cateindex = e.currentTarget.dataset.cateindex;//获取当前选择品类的index

    //获取价格参数
    var selectedValue = e.detail.value; //多列选中出来的数组
    var priceIndex = selectedValue[0];//选中价格索引
    var numberIndex = selectedValue[1];//选中件数索引    
    let priceList = this.data.tagList[this.data.currentTab].list[cateindex].price[0];//获取原来价格的list
    let priceSlected = priceList[priceIndex]; //选择的价格    
    let numberList = this.data.tagList[this.data.currentTab].list[cateindex].price[1];// 获取原来件数的list
    let numberSlected = numberList[numberIndex]; //选择的数量

    var item = {
      sortid: sortid,//父分类id
       sortname: sortname,//父分类名称
      cateid: cateid,//品类id
      catename: catename,//品类名称
       price: priceSlected,
      numbers: numberSlected,
      successSubmit:1,
      otherCateId:0,// 其他区id 其他区表单提交需要        

    }
         //取消选择用
    this.data.priceSlected = priceSlected
    this.data.numberSlected = numberSlected
    // console.log(item)
    // console.log(this.data.selectedCateList.indexOf(item))
    var findedItem = this.data.selectedCateList.find(t => t.sortid == sortid && t.cateid == cateid )
    // console.log(findedItem)
    if (findedItem != undefined){
      var findIndex = this.data.selectedCateList.indexOf(findedItem)
      this.data.selectedCateList.splice(findIndex,1)
    }
    this.data.selectedCateList.push(item);
    // console.table(this.data.selectedCateList)
  },
  // 底部选择器取消时触发
  cancelBlackGround: function(e) {   
    console.log(e)
    var sortid = e.currentTarget.dataset.sortid;//获取父分类id      
    var cancelcateid = e.currentTarget.dataset.cateid; //获取品类id
    var cateindex = e.currentTarget.dataset.cateindex;
   
    this.data.tagList[this.data.currentTab].list[cateindex].ischeck = false
    //移除数组相同元素
    var cancelItem = this.data.selectedCateList.find(t => t.sortid == sortid && t.cateid == cancelcateid)
    console.log(cancelItem)
    if (cancelItem != undefined) {    
      var cancelIndex = this.data.selectedCateList.indexOf(cancelItem)
      console.log(cancelIndex)
      this.data.selectedCateList.splice(cancelIndex, 1)
    }
    // console.table(this.data.selectedCateList)
    this.setData({
      tagList: this.data.tagList
    })
  },
  // 其他区点击提交表单
  othertijiao:function(e){    
    var that = this
    wx.setStorage({
      key: 'IsClickContactOther',
      data: '1',
    })
    var mohuanUser = app.globalData.mohuanUser
     that.setData({
       mohuanUser: app.globalData.mohuanUser
     })
    var token = mohuanUser["X-AUTH-TOKEN"]
    console.log(mohuanUser)
    // 从缓存获取推荐人手机号
    var phoneNo= wx.getStorageSync('phoneNo')
  // 获取品类id
    var othercateid = e.currentTarget.dataset.othercateid;  
    // console.log('1222222222222') 
    var paramsText = {
      recommender: phoneNo,//推荐人手机号 
      memberId: mohuanUser.memberId,//用户id 
      demandId: that.data.requirementId,//需求id 
      categoryId: othercateid,//品类id wu
      userCategory: that.data.otherInput1,//用户所选的品类
      totalBudget: that.data.otherInput2,//总的金额     
      dressOccasion: that.data.otherInput3,//工作changjing
      remark: that.data.otherInput4,//备注
    }
    console.log(paramsText)
    var mohuanUser = app.globalData.mohuanUser
    var token = mohuanUser["X-AUTH-TOKEN"]
    console.log(token)
    var url = 'https://www.magicmanufactory.com/c2mwebservice/api/v2/customization/addDemand'
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      data: paramsText,
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'X-AUTH-TOKEN': token,
      },
      success(res) {
        console.log(res.data)
        wx.showToast({
          title: '提交成功',
          duration:500
        })    
        // 清楚选择缓存列表
        wx.removeStorageSync('otherCateId')
      }
    })


  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },


// 确认并提交需求跳转到定制页面==男装和婚礼区页面
submitSelectedList:function(){
  console.log(this.data.selectedCateList)
  // console.table(this.data.selectedCateList) 
  wx.setStorage({
    key: 'selectedCateList',
    data: this.data.selectedCateList,
    success: function (res) {
      wx.showToast({
        title: '提交成功',       
        duration: 500,//显示时长
        mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon: 'success', //图标，支持"success"、"loading"
      })
      setTimeout(
        function(){
          wx.switchTab({
            url: '/pages/customAssistant/customAssistant',
          })
        }     
        , 1000);    
    }
  })
},

  // 绑定备注表单事件
  bindremarksInput1: function (e) {
    this.setData({
      otherInput1: e.detail.value,
      otherInput11:1
    })
    console.log('===='+this.data.otherInput11)
    if (this.data.otherInput11 == 1 && this.data.otherInput22 == 1 && this.data.otherInput33 == 1 && this.data.otherInput44 == 1) {
      this.setData({
        disabled: false
      })
    }
   
  },
  // 绑定备注表单事件
  bindremarksInput2: function (e) {
    this.setData({
      otherInput2: e.detail.value,
      otherInput22: 1
    })
    console.log('====' + this.data.otherInput22)
    if (this.data.otherInput11 == 1 && this.data.otherInput22 == 1 && this.data.otherInput33 == 1 && this.data.otherInput44 == 1) {
      this.setData({
        disabled: false
      })
    }
  
  },
  // 绑定备注表单事件
  bindremarksInput3: function (e) {
    this.setData({
      otherInput3: e.detail.value,
      otherInput33: 1
    })
     console.log('====' + this.data.otherInput11)
    console.log('====' + this.data.otherInput22)
    console.log('====' + this.data.otherInput33)
  
    if (this.data.otherInput11 == 1 && this.data.otherInput22 == 1 && this.data.otherInput33 == 1 ) {
      this.setData({
        disabled: false
      })
      console.log('===' + this.data.disabled)
    }
   
  },
  // 绑定备注表单事件
  bindremarksInput4: function (e) {
    this.setData({
      otherInput4: e.detail.value,     
    })

   
  },


})